<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <%@include file="../common/head.jsp" %>
	<script type="text/javascript">
		$(function(){ 
			$("#code").focus();
			$("#code").keypress(function(e) {  
			    // 回车键事件
		       if(e.which == 13) {
		    	   removeAll();
		    	   search();
		       }
		    });
			$("#materialId").change(function(){
				$("#material").html($(this).find("option:selected").attr("info"));
				$("#msg").html('');
				$("#qty").val('');
			});
		});
		
		var locatorCode = null;
		back2ServerData = null;
		
		var locatProducts = [];
		function search(){
			var data = {};
			data.value = $("#code").val();

			if(data.code == ''){
				$("#code").focus();
				$("#msg").html("<div style='color: #ff0000;'>条码号不能为空</div>");
				return;	
			}
			back2ServerData = null;
			$.ajax({
				type : 'POST',
				url : "<%=request.getContextPath()%>/inventory/search",
				data : data,
				success : function(responseData) {
					back2ServerData = responseData;
					if(responseData.result) {
						locatorCode = data.value;
						$("#msg").html('');
						var arr = responseData.rows;
						var tableView = $("#tableView");
						tableView.empty();
						
						locatProducts = arr;
						for(var i = 0 ; i < arr.length ; i ++){
							var obj = arr[i];
							var html = '<tr><td class="hideId" style="display:none">' + obj.PID + '</td>';
							html += '<td>' + obj.NAME + '<br>(' + obj.PID + ')</td>';
							html += '<td class="qty">' + obj.QTY + '</td>';
							html += '<td><input class="qtyOk" id="qtyOk_' + i + '" value="' + obj.QTY + '" style="width: 40px;text-align: center;"' +
									' onkeydown="if(event.keyCode==13) focusNextInput(\'qtyOk_' + (i + 1) + '\');"/></td>';
							html += '<td class="uname">' + obj.UNAME + '</td></tr>';
							
							tableView.append(html);
						}
						if(arr.length > 0){
							$("#qtyOk_0")[0].select();
							$("#msg").html("");
						} else {
							$("#msg").html("<div style='color: #ff0000;'>无库存</div>");
						}
					}else{
						$("#qtyOk_0").focus();
						$("#msg").html("<div style='color: #ff0000;'>" + responseData.msg + "</div>");
					}
					
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert(errorThrown);
				},
				dataType : 'json'
			});
		}
		var subBool = false;
		
		function focusNextInput(id) {
			try {
				$("#" + id)[0].select();
			} catch (e) {
			}
		}
		
		function submit(){
			var data = {};
			data.code = $("#code").val();
			if(data.code == ''){
				$("#msg").html("<div style='color: #ff0000;'>库位号不能为空</div>");
				return;	
			}
			if(back2ServerData == null || locatProducts.length == 0){
				$("#msg").html("<div style='color: #ff0000;'>请先查询物料确认后再提交</div>");
				return;
			}
			var ids = [];
			var qtys = [];
			var qtyOks = [];
			for (var i = 0;i < locatProducts.length;i ++) {
				ids.push(locatProducts[i].PID);
				qtys.push(locatProducts[i].QTY);
				var qty = $("#qtyOk_" + i).val() * 1;
				if (qty < 0 || isNaN(qty)) {
					$("#msg").html("<div style='color: #ff0000;'>" + locatProducts[i].PID + "数量填写不合理！</div>");
					return;
				}
				qtyOks.push(qty);
			}
			$("#msg").html("");

			data.ids = ids;
			data.qtyOks = qtyOks;
			data.qtys = qtys;
			
			data.locatorCode =  locatorCode;
			data.wareHouseId = back2ServerData.wareHouseId;
			$.ajax({
				type : 'POST',
				url : "<%=request.getContextPath()%>/inventory/submit",
				data : data,
				success : function(responseData) {
					if(responseData.result){
						$("#msg").html('提交成功');
						removeAll();
						$("#code").val('');
						$("#code").focus();
					}else{
						$("#msg").html("<div style='color: #ff0000;'>" + responseData.msg + "</div>");
						$("#code").val('');
						$("#code").focus();
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert(errorThrown);
				},
				dataType : 'json'
			});
		}
		function removeAll(){
			$("#tableView").empty();
			$("#msg").html("");
			subBool = false;
			locatorCode = null;
			locatProducts = [];
		}
	</script>
</head>
<body>
	<table style="width: 100%" cellpadding="3">
		<tr>
			<td colspan="2" style="background-color: #4E81ac;color: #ffffff;">
				<div style="float: left;margin: 2px;font-size: 18px;">盘点录入</div>
				<div style="float: right;margin-right: 10px;">
				</div>
			</td>
		</tr>
		<tr>
			<td align="center">库位号:</td>
			<td style="width: 220px">
				<input type="text" id="code" value="" maxlength="32"/>
				<input type="hidden" id="id" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
			<table class="detail_table">
				<THEAD>
					<tr>
						<th>物料信息</th>
						<th style="width: 56px">账务数量</th>
						<th style="width: 56px" nowrap="nowrap">盘点数量</th>
						<th style="width: 56px">单位</th>
					</tr>
				</THEAD>
				<tbody id="tableView">
				</tbody>
			</table>
			</td>
		</tr>
		<tr>
			<td align="right">提示:</td>
			<td id="msg"></td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<input type="button" name="submit" class="okButton" value="确定" onclick="javascript:submit();"/>
				&nbsp;&nbsp;
				<input type="button" name="close" class="cancelButton" value="关闭" onclick="javascript:history.back();"/>
			</td>
		</tr>
	</table>
</body>
</html>